/**
 * 深度拷贝
 * @param {*} source 
 * @returns 
 */
export function deepClone(source) {
  if(typeof source !== 'object' || source === null) {
    return source
  }
  const target = Array.isArray(source) ? [] : {}
  for(let key in source) {
    if(source.hasOwnProperty(key)) {
      if(source[key] && typeof source[key] === 'object') {
        target[key] = deepClone(source[key])
      } else {
        target[key] = source[key]
      }
    }
  }
  return target
}

/**
 * 生成uuid
 * @returns 
 */
export function getUuid() {
  const temp_url = URL.createObjectURL(new Blob())
  const uuid = temp_url.toString()
  URL.revokeObjectURL(temp_url) //释放这个url
  return uuid.substring(uuid.lastIndexOf('/') + 1)
}

/**
 * 文件下载
 * @param {File} blob 
 * @param {string} filename 
 */
export function download(blob, filename = 'WFT.xlsx') {
  const link = document.createElement('a')
  const URL = window.URL || window.webkitURL;
  const href = URL.createObjectURL(new Blob([blob]))
  link.href = href
  link.download = filename
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
  // URL.revokeObjectURL(href) // 释放掉blob对象
}

/**
 * 获取静态资源
 * @param {*} url 
 * @returns 
 */
export function getStaticResource(url) {
  return require(`../static/${url}`)
}

/**
 * 检验数据类型
 * @param {*} obj 
 * @returns 
 */
export function getObjType(obj) {
  let toString = Object.prototype.toString
  let map = {
    '[object Boolean]': 'boolean',
    '[object Number]': 'number',
    '[object String]': 'string',
    '[object Function]': 'function',
    '[object Array]': 'array',
    '[object Date]': 'date',
    '[object RegExp]': 'regExp',
    '[object Undefined]': 'undefined',
    '[object Null]': 'null',
    '[object Object]': 'object'
  }
  if (obj instanceof Element) {
    return 'element'
  }
  return map[toString.call(obj)]
}

/**
 * 动画组件类名
 * @param {*} classNames 
 * @returns 
 */
export const animationsClass = (classNames) => {
  if(classNames?.length) {
    return `animate__animated animate__${classNames[0]}`
  }
  return ''
}